#include "messageshowarea.h"

using namespace model;

////////////////////////////////////////
/// 消息的区域
//////////////////////////////////////////
MessageShowArea::MessageShowArea() {

    setStyleSheet("QWidget{border:none;}");
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setWidgetResizable(true);

    verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width: 2px; background-color: rgb(240,240,240;)}");
    horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{height:0px;}");

    container = new QWidget();
    setWidget(container);

    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout->setSpacing(0);
    vLayout->setContentsMargins(0,0,0,0);
    container->setLayout(vLayout);


;}


////////////////////////////////////////
/// 消息Item
//////////////////////////////////////////
MessageItem::MessageItem(bool isLeft){

}

// 通过工厂模式构造函数
MessageItem* makeMessageItem(bool isLeft, const Message& message){
    MessageItem* messageItem = new MessageItem(isLeft);
    QGridLayout* gLayout = new QGridLayout();
    gLayout->setSpacing(0);
    gLayout->setContentsMargins(0,0,0,0);

    messageItem->setMinimumHeight(100);
    messageItem->setLayout(gLayout);

    //创建头像
    QPushButton *avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(40,40);
    avatarBtn->setIconSize(QSize(40,40));
    avatarBtn->setIcon(message.sender.avatar);
    if(isLeft){
        gLayout->addWidget(avatarBtn, 0,0,2,1, Qt::AlignTop | Qt::AlignLeft);
    }else{
        gLayout->addWidget(avatarBtn, 0,1,2,1, Qt::AlignTop | Qt::AlignLeft);
    }

    // 创建呢称和时间
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(message.sender.nickname + " | " + message.time);
    nameLabel->setStyleSheet("QLabel{font-size:12px; color: rag(178,178,178);}");
    if(isLeft){
        gLayout->addWidget(nameLabel, 0, 1);
    }else{
        gLayout->addWidget(nameLabel, 0,0, Qt::AlignRight);
    }

    QWidget* contentWidget = nullptr;
    switch(message.messageType){
        case TEXT_TYPE:
            contentWidget = makeTextMessageItem();
            break;
        case IMAGE_TYPE:
            contentWidget = makeImageMessageItem();
            break;
        case FILE_TYPE:
            contentWidget = makeFileMessageItem();
            break;
        case VOICE_TYPE:
            contentWidget = makeSpeechMessageItem();
            break;
        default:
            LOG() << "错误的消息类型，current message = " << message.messageType;
    }

    if(isLeft){
        gLayout->addWidget(contentWidget, 1, 1);
    }else{
        gLayout->addWidget(contentWidget, 1, 0);
    }
    return messageItem;

}

// 添加工厂函数
QWidget* MessageItem::makeTextMessageItem(bool isLeft, const QString& text){

}
QWidget* MessageItem::makeImageMessageItem(bool isLeft, const QString& fileId, const QByteArray& content){

}
QWidget* MessageItem::makeFileMessageItem(bool isLeft, const QString& message){

}
QWidget* MessageItem::makeSpeechMessageItem(bool isLeft, const QString& message){

}

////////////////////////////////////////
/// 消息Item中的文本消息的正文部分
//////////////////////////////////////////

MessageContentLabel::MessageContentLabel(const QString& text, bool isLeft)
    :_isLeft(isLeft)
{
    QFont font;
    font.setFamily("微软雅黑");
    font.setPixelSize(16);
    _label = new QLabel();
    _label->setFont(font);
    _label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    _label->setWordWrap(true);
    _label->setStyleSheet("QLabel{padding: 0 10px; line-height: 1.2; }");

}

void MessageContentLabel::painEvent(QPaintEvent* event){
    QObject* obj = parent();
    if(!obj->isWidgetType()){
        return ;
    }
    QWidget* parent = dynamic_cast<QWidget*>(obj);

    // 约定文字消息每行为行宽的0.6
    int width = parent->width() * 0.6;

    // 计算当前文本，如果作为一行的话，有多宽
    QFontMetrics metrics(_label->font());
    int totalWidth = metrics.horizontalAdvance(_label->text());

    // 40是左右各有20px的边距
    int rows = (totalWidth / (width - 40) + 1 );
    if(rows == 1){
        width = totalWidth + 40;
    }

    int height = rows * (_label->font().pixelSize() * 1.2) + 20;
}
